package com.apexsoft.assets.base.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.apexsoft.assets.base.PagedArrayList;
import com.apexsoft.assets.base.model.Columns;
import com.apexsoft.assets.base.model.Result;
import com.apexsoft.assets.base.response.AResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.beanutils.BeanUtils;

import javax.validation.constraints.NotNull;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.*;

/**
 * @author guojieming1
 * @date 2021-1-26
 */
public class ResultUtil {
    public static final int CODE_SUCCESS = 1;
    public static final int CODE_ERROR = -1;

    public static <T> AResponse<T> success() {
        AResponse<T> response = new AResponse<>();
        response.setCode(CODE_SUCCESS);
        return response;
    }

    public static <T> AResponse<T> success(List<T> data) {
        AResponse<T> response = success();
        response.setRecords(data);
        if(data!=null && !data.isEmpty()){
            if(data instanceof PagedArrayList && ((PagedArrayList<?>) data).getTotal()>data.size()){
                response.setTotalRecord(((PagedArrayList<?>) data).getTotal());
            }else{
                response.setTotalRecord(data.size());
            }
        }
        return response;
    }

    public static <T> AResponse<T> success(Collection<T> data) {
        AResponse<T> response = success();
        List<T> list = new LinkedList<>(data);
        response.setRecords(list);
        if(data!=null && !data.isEmpty()){
            if(data instanceof PagedArrayList && ((PagedArrayList<?>) data).getTotal()>data.size()){
                response.setTotalRecord(((PagedArrayList<?>) data).getTotal());
            }else{
                response.setTotalRecord(data.size());
            }
        }
        return response;
    }

    public static <T> AResponse<T> success(Page<T> data){
        AResponse<T> response = success(data.getRecords());
        response.setPageSize(data.getSize());
        response.setCurrentPage(data.getCurrent());
        response.setTotalPage(data.getPages());
        response.setTotalRecord(data.getTotal());
        return response;
    }

    public static <T> AResponse<T> success(List<T> data, int currentPage, int pageSize){
        AResponse<T> response = success(data);
        response.setPageSize(pageSize);
        response.setCurrentPage(currentPage);
        response.setTotalPage(calculateTotalPage(pageSize, response.getTotalRecord()));
        return response;
    }

    public static long calculateTotalPage(long pageSize, long totalRecord){
        if(pageSize<1) {
            return -1;
        }
        if(totalRecord<0) {
            return -1;
        }
        if(totalRecord==0) {
            return 0;
        }
        return totalRecord/pageSize + (totalRecord%pageSize==0?0:1);
    }

    @Deprecated
    public static <T> AResponse<T> successRecords(List<T> list) {
        AResponse<T> response = success();
        if(list!=null && !list.isEmpty()) {
            response.setRecords(list);
        }
        return response;
    }

    public static <T> AResponse<T> success(Map<String, Object> data, T t) throws InvocationTargetException, IllegalAccessException {
        AResponse<T> response = success();
        if(data!=null && !data.isEmpty()){
            BeanUtils.populate(t, data);
            response.setData(t);
        }
        return response;
    }

    @Deprecated
    public static <T> AResponse<T> successData(Map<String, Object> data, T t) throws InvocationTargetException, IllegalAccessException {
        AResponse<T> response = success();
        if(data!=null && !data.isEmpty()){
            BeanUtils.populate(t, data);
            response.setData(t);
        }
        return response;
    }

    public static <T> AResponse<T> successData(T data) {
        AResponse<T> response = success();
        response.setData(data);
        return response;
    }

    public static <T> AResponse<T> successNote(String note) {
        AResponse<T> response = success();
        response.setNote(note);
        return response;
    }

    public static <T> AResponse<T> result(int code, String note) {
        AResponse<T> response = new AResponse<>();
        response.setCode(code);
        response.setNote(note);
        return response;
    }

    public static <T> AResponse<T> result(Result result) {
        AResponse<T> response = new AResponse<>();
        response.setCode(result.getCode());
        response.setNote(result.getNote());
        return response;
    }

    public static <T> AResponse<T> error(String note) {
        return result(CODE_ERROR, note);
    }

    public static <T> AResponse<T> result(@NotNull com.apexsoft.aas.service.model.AResponse aResponse, Class<T> T){
        AResponse<T> response = new AResponse<>();
        response.setTotalRecord(aResponse.getTotalRecord());
        response.setTotalPage(aResponse.getTotalPage());
//        response.setCurrentPage(aResponse.ge);
//        response.setPageSize(aResponse.get);
        if(aResponse.getRecords()!=null /*&& !aResponse.getRecords().isEmpty()*/) {
            response.setRecords(aResponse.getRecordsEx(T));
        }
        if(aResponse.getData()!=null && !aResponse.getData().isEmpty()) {
            response.setData(aResponse.getDataEx(T));
        }
        if(Objects.nonNull(aResponse.getHeads().get("columns"))) {
            //转化为list
            Map map = aResponse.getHeads();
            JSONArray jsonArray = (JSONArray) map.get("columns");
            List<Columns> columnsList = JSONObject.parseArray(jsonArray.toJSONString(), Columns.class);
            response.setColumns(columnsList);
        }
        response.setDuration(Long.parseLong(aResponse.getDuration()));
        response.setErrCode(String.valueOf(aResponse.getErrcode()));
        response.setCode(aResponse.getCode());
        response.setNote(aResponse.getNote());
        return response;
    }


    //oracle.sql.Clob类型转换成String类型

    public static String ClobToString(Clob clob) throws SQLException, IOException {

        String reString = "";
        Reader is = clob.getCharacterStream();// 得到流
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        return reString;
    }
}
